using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class UserControls_CtrlCreditCard : System.Web.UI.UserControl
{
    protected Functions func;
    protected Card ca;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            loadCardType();
            loadInfoCreditCard();
        }
    }
    private void loadInfoCreditCard()
    {
        int ID = Convert.ToInt32(Session["ID"].ToString());
        ca = new Card();
        ca = ca.GetUserCard(ID);
        if (ca != null)
        {
            txtCardName.Text = ca.userCard;
            ddlCardType.SelectedValue = ca.cardType;
            DateTime tmpDate = ca.CardExpire;
            txtCardDate.Text = tmpDate.Day.ToString();
            if (Convert.ToInt32(tmpDate.Month.ToString()) < 10)
            {
                ddlCardMonth.SelectedValue = "0" + tmpDate.Month.ToString();
            }
            else
            {
                ddlCardMonth.SelectedValue = tmpDate.Month.ToString();
            }
            txtCardYear.Text = tmpDate.Year.ToString();

        }
    }
    private void loadCardType()
    {
        ca = new Card();
        DataTable tmp = ca.getAllTypeCard();
        if (tmp.Rows.Count > 0)
        {
            for (int i = 0; i < tmp.Rows.Count; i++)
            {
                ddlCardType.Items.Add(new ListItem(tmp.Rows[i]["name"].ToString(), tmp.Rows[i]["id"].ToString()));
            }
        }
    }
    //validate credit card number
    protected void txtCardCodeCustomValidate_ServerValidate(object source, ServerValidateEventArgs args)
    {
        BankServices.Service checkAccount = new BankServices.Service();
        func = new Functions();
        DateTime expriredDate = func.getDateTime(txtCardDate.Text, ddlCardMonth.SelectedValue, txtCardYear.Text);
        if (!checkAccount.CheckAccountExists(txtCardName.Text, txtCardCode.Text, expriredDate, ddlCardType.SelectedItem.Text))
        {
            args.IsValid = false;
        }
        else
        {
            args.IsValid = true;
        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            ca = new Card();
            ca.userID = Convert.ToInt32(Session["ID"].ToString());
            ca.userCard = txtCardName.Text;
            ca.passwordCard = ca.Card_encode_toByte(txtCardCode.Text);
            ca.cardType = ddlCardType.SelectedValue;
            DateTime expired = func.getDateTime(txtCardDate.Text, ddlCardMonth.SelectedValue, txtCardYear.Text);
            ca.CardExpire = expired;
            if (ca.updateCard())
            {
                Response.Redirect("./Default.aspx");
            }
        }
    }
}
